package com.seadragon.apps.doctorcard.validator;

import static com.seadragon.apps.doctorcard.model.DoctorCardConstants.ERROR_CODE_REGISTER_USERNAME_EMPTY;
import static com.seadragon.apps.doctorcard.model.DoctorCardConstants.REGISTER;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.seadragon.apps.doctorcard.model.Contact;

@Component("contactValidator")
public class ContactValidator implements Validator {

	@Override
	public boolean supports(Class<?> arg0) {
		return Contact.class.isAssignableFrom(arg0);
	}

	@Override
	public void validate(Object arg0, Errors arg1) {
		Contact contact = (Contact) arg0;
		if ((contact.getEmail() == null || contact.getEmail().length() == 0)
				&& (contact.getMobile() == null || contact.getMobile().length() == 0)
				&& (contact.getDoctorCardId() == null || contact.getDoctorCardId().length() == 0)) {
			arg1.reject(REGISTER, Integer.toString(ERROR_CODE_REGISTER_USERNAME_EMPTY));
		}
	}
}
